package com.hhs.storage.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.hhs.storage.bean.JxcMenu;
import com.hhs.storage.bean.JxcType;
import com.hhs.storage.bean.Result;
import com.hhs.storage.bean.TreeMenu;
import com.hhs.storage.service.JxcTypeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author shadow
 * @since 2018-10-18
 */
@RestController
@RequestMapping("/do/type")
public class JxcTypeController {

    @Resource
    JxcTypeService jxcTypeService;


    @RequestMapping("getAll")
    public List<JxcType> getAll() {
        List<JxcType> list = jxcTypeService.selectList(new EntityWrapper<JxcType>().orderBy("create_time_"));
        return list;
    }

    @RequestMapping(value = "{id}", method = RequestMethod.DELETE)
    public Map<String, Object> dele(@PathVariable("id") String id) {
        List<JxcType> sonList = jxcTypeService.selectList(new EntityWrapper<JxcType>().eq("pid_", id));
        if(sonList.size()>0){
            return Result.failJsonResp("此类别有下级节点，无法删除");
        }
        jxcTypeService.delete(new EntityWrapper<JxcType>().eq("id_", id));
        return Result.sucJsonResp(null);


    }

    @RequestMapping("edit")
    public Map<String, Object> edit(JxcType jxcType) {
        if (StringUtils.isEmpty(jxcType.getId())) {
            jxcType.setCreateTime(new Date());
        }
        jxcTypeService.insertOrUpdate(jxcType);
        return Result.sucJsonResp(jxcType);

    }
}

